
function transmitParam(role_id){
	initForm(role_id)
	initRoleInfo(role_id);
}

function initForm(role_id){
	var form = layui.form
	,layer = layui.layer
	,tree = layui.tree
	,util = layui.util;
	
	$.ajax({
		 type : 'get'
		 ,dataType : 'json'
		 ,async : false
		 ,url : access_url+'/cache/loadcachedata/getdictsbypcode?parent_code=1000'
		 ,success : function(data){
			console.log("data",data);
			if(data.code != 200){
				layer.msg(data.msg, {icon: 5});
				return;
			}
			
			var dictlist = data.retlist;
			
			var dictLen = dictlist.length;
			
			var roleLimitHtml = '';
			
			for (var i = 0; i < dictLen; i++) {
				var dict = dictlist[i];
				var htmlModel = '<option value="{word_value}">{word_mean}</option>'
				var opHtml = htmlModel.format(dict);
				roleLimitHtml += opHtml;
			}
			
			$('#role_data_limit').append(roleLimitHtml);
			
			form.val('role-form');
	 	 }
	})
	
	$.ajax({
		type : 'get'
		,url : access_url+'/cache/loadcachedata/getmenutreefromcache'
		,dataType:'json'
		,success : function(data){
			if(200!=data.code){
				layer.msg(data.msg, {icon: 5});
				return;
			}
			
			var menuTree = data.retlist;
			
			tree.render({
				elem: '#menuTree'
				,data: menuTree
				,id: 'menuTree'
				,showCheckbox: true  //是否显示复选框
			});
			console.log("role_id",role_id);
			$.ajax({
				type : 'get'
				,url : access_url+'/system/systemmanage/getrolemenurelation'
				,dataType:'json'
				,data : {
					role_id : role_id
				}
				,success : function(data){
					if(200!=data.code){
						layer.msg(data.msg, {icon: 5});
						return;
					}
				
					var menuIds = data.retlist;
					
					tree.setChecked('menuTree', menuIds); //勾选指定节点
				}
			})
		}
	})
	
	util.event('lay-demo',{
		updateRole : function(othis){
			var formData = form.val('role-form');
			var role_name = formData.role_name;
			var role_data_limit = formData.role_data_limit;
			var role_level = formData.role_level;
			var role_code = formData.role_code;
			
			var checkedData = tree.getChecked('menuTree');
			
			var menus = [];
			 
			if(!isNull(checkedData)){
				 
				menus = revertMenuNodeToArray(checkedData);
				 
				console.log("endMenus",menus);
			}
			
			layer.load(2);
			$.ajax({
				 type : 'post'
				 ,dataType : 'json'
				 ,url : access_url+'/system/systemmanage/updaterole'
				 ,contentType : 'application/json;charset=UTF-8'
				 ,data : JSON.stringify({
					 role_id : role_id
					 ,role_name : role_name
					 ,role_data_limit : role_data_limit
					 ,role_level : role_level
					 ,role_code : role_code
					 ,menus : menus
				 })
				 ,success : function(data){
					layer.closeAll('loading');
					if(data.code != 200){
						layer.msg(data.msg, {icon: 5});
						return;
					}
					parent.layer.alert(data.msg,{icon:6});
					parent.loadTable();
					parent.layer.closeAll('iframe');
				 }
			 	 ,error : function(){
			 		layer.closeAll('loading');
			 		layer.msg('更新失败,请联系管理员', {icon: 5});
			 	 }
			 })
		}
	})
}

function initRoleInfo(role_id){
	var form = layui.form
	  ,layer = layui.layer
	  ,tree = layui.tree
	  ,util = layui.util;
	
	$.ajax({
		 type : 'get'
		 ,url : access_url+'/system/systemmanage/getallroles'
		 ,dataType:'json'
		 ,data : {
			 role_id : role_id
		 }
		 ,success : function(data){
			 if(200!=data.code){
				layer.msg(data.msg, {icon: 5});
			 	return;
			 }
			 
			 var retlist = data.retlist;
			 
			 if(isNull(retlist)){
				 layer.msg('未获取到角色信息', {icon: 5});
				 return;
			 }
			 
			 var roleInfo = retlist[0];
			 
			 var role_name = roleInfo.role_name;
			 var role_level = roleInfo.role_level;
			 var role_data_limit = roleInfo.role_data_limit;
			 var role_code = roleInfo.role_code;
			 
			 form.val('role-form',{
				 "role_name" : role_name
				 ,"role_data_limit" : role_data_limit
				 ,"role_level" : role_level
				 ,"role_code" : role_code
			 });
		 }
	 })
}

function revertMenuNodeToArray(checkedData){
	var menus = [];
	
	var checkedLen = checkedData.length;
	 
	for (var i = 0; i < checkedLen; i++) {
		var menu_id = checkedData[i].id;
		
		var menuNode = `{"menu_id":"${menu_id}"}`;
		
		menus.push(JSON.parse(menuNode));

		var children = checkedData[i].children;
		
		if(!isNull(children)){
			var menuArray = revertMenuNodeToArray(children);
			
			console.log("menuArray",menuArray);
			menus = menus.concat(menuArray);
			console.log("menus",menus);
		}
	}
	return menus;
	
}